package com.example.echoewish.ui.my_activity.friend_request_activity;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.example.echoewish.econstant.Constant;
import com.example.echoewish.econstant.UserInfo;
import com.example.echoewish.pojo.MyPreConcernRequest;
import com.example.echoewish.repository.PreConcernRepository;
import com.example.echoewish.util.LogUtil;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FriendRequestViewModel extends ViewModel {
    MutableLiveData<Integer>responseMutableLiveData;
    MutableLiveData<Integer> acceptMutableLiveData;
    MutableLiveData<Integer> refuseMutableLiveData;
    MutableLiveData<List<MyPreConcernRequest>> myPreConcernRequestMutableLiveData;

    public  static  final int ACCEPT_SUCCESS=3;
    public  static  final int ACCEPT_FAIL=4;
    public static  final int REFUSE_SUCCESS=5;

    public FriendRequestViewModel()
    {
        responseMutableLiveData=new MutableLiveData<>();
        acceptMutableLiveData=new MutableLiveData<>();
        refuseMutableLiveData=new MutableLiveData<>();
        myPreConcernRequestMutableLiveData=new MutableLiveData<>();
        myPreConcernRequestMutableLiveData.setValue(new LinkedList<>());
    }

    public void getFriendRequest()
    {
        PreConcernRepository.getInstance().getMyPreConcernRequests(UserInfo.getInstance().UserId,new GetFriendRequestCallBack());
    }

    public void acceptFriendRequest()
    {
        PreConcernRepository.getInstance().acceptConcernRequest(acceptMutableLiveData.getValue(),UserInfo.getInstance().UserId,new AcceptFriendRequestCallBack());
    }

    public void refuseFriendRequest()
    {
        PreConcernRepository.getInstance().refuseConcernRequest(refuseMutableLiveData.getValue(),UserInfo.getInstance().UserId,new RefuseFriendRequestCallBack());
    }

    private class GetFriendRequestCallBack implements Callback<List<MyPreConcernRequest>>
    {
        @Override
        public void onResponse(Call<List<MyPreConcernRequest>> call, Response<List<MyPreConcernRequest>> response) {
            myPreConcernRequestMutableLiveData.postValue(response.body());
        }

        @Override
        public void onFailure(Call<List<MyPreConcernRequest>> call, Throwable t) {
            myPreConcernRequestMutableLiveData.postValue(null);
            new LogUtil().i(LogUtil.CALLBACK_TAG,"获取朋友请求 回调出现错误");
        }
    }

    private class AcceptFriendRequestCallBack implements Callback<ResponseBody>
    {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                if(response.body().string().equals(Constant.HTTPS_GET_CALLBACK_SUCCESS))
                responseMutableLiveData.postValue(ACCEPT_SUCCESS);
                else
                    responseMutableLiveData.postValue(Constant.HTTPS_GET_CALLBACK_FAIL_MESSAGE);
            } catch (IOException ioException) {
                responseMutableLiveData.postValue(Constant.HTTPS_GET_CALLBACK_FAIL_MESSAGE);
                ioException.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            responseMutableLiveData.postValue(Constant.HTTPS_GET_CALLBACK_FAIL_MESSAGE);
            new LogUtil().i(LogUtil.CALLBACK_TAG,"接收朋友请求 回调出现错误");
        }
    }

    private class RefuseFriendRequestCallBack implements Callback<ResponseBody>
    {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            try {
                if(response.body().string().equals(Constant.HTTPS_GET_CALLBACK_SUCCESS))
                    responseMutableLiveData.postValue(REFUSE_SUCCESS);
                else
                    responseMutableLiveData.postValue(Constant.HTTPS_GET_CALLBACK_FAIL_MESSAGE);
            } catch (IOException ioException) {
                responseMutableLiveData.postValue(Constant.HTTPS_GET_CALLBACK_FAIL_MESSAGE);
                ioException.printStackTrace();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            responseMutableLiveData.postValue(Constant.HTTPS_GET_CALLBACK_FAIL_MESSAGE);
            new LogUtil().i(LogUtil.CALLBACK_TAG,"拒绝朋友请求 回调出现错误");
        }
    }
}
